package com.dormitory.ui;

import com.dormitory.model.CheckIn;
import com.dormitory.model.Room;
import com.dormitory.model.Student;
import com.dormitory.service.RoomService;
import com.dormitory.service.StudentService;
import com.dormitory.service.impl.RoomServiceImpl;
import com.dormitory.service.impl.StudentServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CheckInFormDialog extends JDialog {
    private JComboBox<Student> studentComboBox;
    private JComboBox<Room> roomComboBox;
    private JTextField checkInDateField;
    private JTextField checkOutDateField;
    private JTextArea remarkArea;
    private boolean confirmed = false;
    private CheckIn checkIn;
    private StudentService studentService = new StudentServiceImpl();
    private RoomService roomService = new RoomServiceImpl();

    public CheckInFormDialog(Window owner, CheckIn checkIn) {
        super(owner, "入住信息", ModalityType.APPLICATION_MODAL);
        this.checkIn = checkIn;
        initUI();
        if (checkIn != null) {
            fillForm(checkIn);
        }
        pack();
        setLocationRelativeTo(owner);
    }

    private void initUI() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 学生
        gbc.gridx = 0; gbc.gridy = 0;
        panel.add(new JLabel("学生："), gbc);
        gbc.gridx = 1;
        studentComboBox = new JComboBox<>();
        loadStudents();
        panel.add(studentComboBox, gbc);

        // 房间
        gbc.gridx = 0; gbc.gridy = 1;
        panel.add(new JLabel("房间："), gbc);
        gbc.gridx = 1;
        roomComboBox = new JComboBox<>();
        loadRooms();
        panel.add(roomComboBox, gbc);

        // 入住日期
        gbc.gridx = 0; gbc.gridy = 2;
        panel.add(new JLabel("入住日期(yyyy-MM-dd)："), gbc);
        gbc.gridx = 1;
        checkInDateField = new JTextField(15);
        panel.add(checkInDateField, gbc);

        // 退宿日期
        gbc.gridx = 0; gbc.gridy = 3;
        panel.add(new JLabel("退宿日期(yyyy-MM-dd)："), gbc);
        gbc.gridx = 1;
        checkOutDateField = new JTextField(15);
        panel.add(checkOutDateField, gbc);

        // 备注
        gbc.gridx = 0; gbc.gridy = 4;
        panel.add(new JLabel("备注："), gbc);
        gbc.gridx = 1;
        remarkArea = new JTextArea(3, 15);
        JScrollPane remarkScroll = new JScrollPane(remarkArea);
        panel.add(remarkScroll, gbc);

        // 按钮
        JPanel btnPanel = new JPanel();
        JButton okBtn = new JButton("确定");
        JButton cancelBtn = new JButton("取消");
        okBtn.addActionListener(e -> onOK());
        cancelBtn.addActionListener(e -> onCancel());
        btnPanel.add(okBtn);
        btnPanel.add(cancelBtn);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(btnPanel, BorderLayout.SOUTH);
    }

    private void loadStudents() {
        studentComboBox.removeAllItems();
        List<Student> students = studentService.getAllStudents();
        for (Student student : students) {
            studentComboBox.addItem(student);
        }
    }

    private void loadRooms() {
        roomComboBox.removeAllItems();
        List<Room> rooms = roomService.getAllRooms();
        for (Room room : rooms) {
            roomComboBox.addItem(room);
        }
    }

    private void fillForm(CheckIn c) {
        // 选中学生
        for (int i = 0; i < studentComboBox.getItemCount(); i++) {
            if (studentComboBox.getItemAt(i).getStudentId().equals(c.getStudentId())) {
                studentComboBox.setSelectedIndex(i);
                break;
            }
        }
        // 选中房间
        for (int i = 0; i < roomComboBox.getItemCount(); i++) {
            if (roomComboBox.getItemAt(i).getRoomId().equals(c.getRoomId())) {
                roomComboBox.setSelectedIndex(i);
                break;
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (c.getCheckInDate() != null) checkInDateField.setText(sdf.format(c.getCheckInDate()));
        if (c.getCheckOutDate() != null) checkOutDateField.setText(sdf.format(c.getCheckOutDate()));
        // 备注
        // remarkArea.setText(c.getRemark()); // 如果CheckIn有remark字段
    }

    private void onOK() {
        Student selectedStudent = (Student) studentComboBox.getSelectedItem();
        Room selectedRoom = (Room) roomComboBox.getSelectedItem();
        if (selectedStudent == null || selectedRoom == null) {
            JOptionPane.showMessageDialog(this, "请选择学生和房间！");
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date checkInDate = null, checkOutDate = null;
        try {
            checkInDate = sdf.parse(checkInDateField.getText().trim());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "入住日期格式错误！");
            return;
        }
        if (!checkOutDateField.getText().trim().isEmpty()) {
            try {
                checkOutDate = sdf.parse(checkOutDateField.getText().trim());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "退宿日期格式错误！");
                return;
            }
        }
        confirmed = true;
        if (checkIn == null) checkIn = new CheckIn();
        checkIn.setStudentId(selectedStudent.getStudentId());
        checkIn.setRoomId(selectedRoom.getRoomId());
        checkIn.setCheckInDate(checkInDate);
        checkIn.setCheckOutDate(checkOutDate);
        checkIn.setStatus(remarkArea.getText().trim()); // 如果CheckIn有remark字段
        dispose();
    }

    private void onCancel() {
        confirmed = false;
        dispose();
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public CheckIn getCheckIn() {
        return checkIn;
    }
} 